const nononoDb_1 = [{ url: '', name: '战地4' }]

const ali213Db_17 = [
  '国土防线',
  '国土防线2：革命',
  '使命召唤4：现代战争',
  '使命召唤5：战争世界',
  '使命召唤6：现代战争2',
  '使命召唤7：黑色行动',
  '使命召唤8：现代战争3',
  '使命召唤9：黑色行动2',
  '使命召唤10：幽灵',
  '使命召唤11：高级战争',
  '使命召唤12：黑色行动3',
  '使命召唤13：无限战争',
  '使命召唤14：二战',
  '幽灵行动：荒野',
  '战地：叛逆连队2',
  '战地3',
  '火星Z：战术基地防御'
]

const indienovaDb_4 = [
  { url: '', name: '冒险村物语' },
  { url: '', name: '纪念碑谷' },
  { url: '', name: '纪念碑谷2' },
  { url: '', name: '数码宝贝物语：网络侦探完全版' },
]

const gamerskyDb_119 = [
  { url: '/2015/Might-and-Magic-Heroes-VII/', name: '魔法门之英雄无敌7' },
  { url: '/2016/might-and-magic-heroes-vii-trial-by-fire/', name: '魔法门之英雄无敌7之火之审判' },
  { url: '/2014/Doom-4/', name: '毁灭战士' },
  { url: '/2009/Mirrors-Edge/', name: '镜之边缘' },
  { url: '/2016/mirrors-edge-catalyst/', name: '镜之边缘：催化剂' },
  { url: '/2007/Mass-Effect/', name: '质量效应1' },
  { url: '/2010/Mass-Effect-2/', name: '质量效应2' },
  { url: '/2012/Mass-Effect-3/', name: '质量效应3' },
  { url: '/2015/mass-effect-andromeda/', name: '质量效应：仙女座' },
  { url: '/2008/Dead-Space/', name: '死亡空间1' },
  { url: '/2011/Dead-Space-2/', name: '死亡空间2' },
  { url: '/2013/Dead-Space-3/', name: '死亡空间3' },
  { url: '/2007/BioShock/', name: '生化奇兵1' },
  { url: '/2010/BioShock-2/', name: '生化奇兵2' },
  { url: '/2013/BioShock-Infinite/', name: '生化奇兵：无限' },
  { url: '/2008/Fallout-3/', name: '辐射3' },
  { url: '/2010/Fallout-NewVegas/', name: '辐射：新维加斯' },
  { url: '/2008/Assassins-Creed/', name: '刺客信条1' },
  { url: '/2010/Assassins-Creed-2/', name: '刺客信条2' },
  { url: '/2011/Assassins-Creed-Brotherhood/', name: '刺客信条：兄弟会' },
  { url: '/2011/Assassins-Creed-Revelations/', name: '刺客信条：启示录' },
  { url: '/2012/Assassins-Creed-3/', name: '刺客信条3' },
  { url: '/2014/Assassins-Creed-Liberation-HD/', name: '刺客信条：解放HD' },
  { url: '/2013/Assassins-Creed-IV-Black-Flag/', name: '刺客信条4：黑旗' },
  { url: '/2014/Assassin-s-Creed-Rogue/', name: '刺客信条：叛变' },
  { url: '/2014/Assassin-s-Creed-Unity/', name: '刺客信条：大革命' },
  { url: '/2015/Quantum-Break/', name: '量子破碎' },
  { url: '/2017/prey-2017/', name: '掠食' },
  { url: '/2016/samurai-warriors-sanada-maru/', name: '战国无双：真田丸' },
  { url: '/2014/Titanfall/', name: '泰坦陨落' },
  { url: '/2014/Titanfall-2/', name: '泰坦陨落2' },
  { url: '/2015/Resident-Evil-7/', name: '生化危机7' },
  { url: '/2008/Naruto-Shippuden-Ultimate-Ninja-Storm/', name: '火影忍者疾风传：究极忍者风暴 1' },
  { url: '/2010/Naruto-Shippuden-Ultimate-Ninja-Storm-2/', name: '火影忍者疾风传：究极忍者风暴 2' },
  { url: '/2013/Naruto-Shippuden-Ultimate-Ninja-Storm-3/', name: '火影忍者疾风传：究极忍者风暴 3' },
  { url: '/2014/Naruto-Shippuden-Ultimate-Ninja-Storm-Revolution/', name: '火影忍者疾风传：究极忍者风暴 革命' },
  { url: '/2015/Naruto-Shippuden-Ultimate-Ninja-Storm-4/', name: '火影忍者疾风传：究极忍者风暴 4' },
  { url: '/2017/naruto-shippuden-ultimate-ninja-storm-4-road-to-boruto/', name: '火影忍者疾风传：究极忍者风暴 4 博人之路' },
  { url: '/2016/Battle-Chasers/', name: '战神：夜袭' },
  { url: '/2009/Wolfenstein/', name: '德军总部' },
  { url: '/2014/Wolfenstein-The-New-Order/', name: '重返德军总部：新秩序' },
  { url: '/2015/Wolfenstein-The-Old-Blood/', name: '重返德军总部：旧血脉' },
  { url: '/2017/wolfenstein-2/', name: '德军总部2：新巨人' },
  { url: '/2014/deponia-the-complete-journey/', name: '德波尼亚：完整旅程' },
  { url: '/2016/attack-on-titan/', name: '进击的巨人' },
  { url: '/2015/kalimba/', name: '克林巴' },
  { url: '/2017/bulletstorm-full-clip-edition/', name: '子弹风暴：完全版' },
  { url: '/2013/Tomb-Raider/', name: '古墓丽影' },
  { url: '/2014/Rise-of-the-Tomb-Raider/', name: '古墓丽影：崛起' },
  { url: '/2004/Far-Cry/', name: '孤岛惊魂' },
  { url: '/2008/Far-Cry-2/', name: '孤岛惊魂2' },
  { url: '/2012/Far-Cry-3/', name: '孤岛惊魂3' },
  { url: '/2013/Far-Cry-3-Blood-Dragon/', name: '孤岛惊魂3：血龙' },
  { url: '/2009/Call-of-Juarez-Bound-in-Blood/', name: '狂野西部：生死同盟' },
  { url: '/2011/Call-of-Juarez-The-Cartel/', name: '狂野西部：毒枭' },
  { url: '/2013/Call-of-Juarez-Gunslinger/', name: '狂野西部：枪手' },
  { url: '/2016/battlefield-1/', name: '战地1' },
  { url: '/2015/Battlefield-S-W-A-T/', name: '战地：硬仗' },
  { url: '/2016/Battlefield-5/', name: '战地5' },
  { url: '/2007/Crysis/', name: '孤岛危机' },
  { url: '/2008/Crysis-warhead/', name: '孤岛危机：弹头' },
  { url: '/2011/Crysis-2/', name: '孤岛危机2' },
  { url: '/2013/Crysis-3/', name: '孤岛危机3' },
  { url: '/2010/Metro-2033/', name: '地铁2033' },
  { url: '/2013/Metro-Last-Light/', name: '地铁：最后的曙光' },
  { url: '/2005/FEAR/', name: '极度恐慌' },
  { url: '/2006/FEAR-Extraction-Point/', name: '极度恐慌：撤离点' },
  { url: '/2007/FEAR-Perseus-Mandate/', name: '极度恐慌：珀尔修斯的指令' },
  { url: '/2009/FEAR-2-Project-Origin/', name: '极度恐慌2：起源计划' },
  { url: '/2011/FEAR3/', name: '极度恐慌3' },
  { url: '/2009/Dragon-Age-Origins/', name: '龙腾世纪' },
  { url: '/2011/Dragon-Age-2/', name: '龙腾世纪2' },
  { url: '/2014/Dragon-Age-Inquisition/', name: '龙腾世纪3：审判' },
  { url: '/2017/darksiders-warmastered-edition/', name: '暗黑血统：战神版' },
  { url: '/2015/darksiders-2-definitive-edition/', name: '暗黑血统2：死亡终极版' },
  { url: '/2014/Darksiders-3/', name: '暗黑血统3' },
  { url: '/2018/onimusha-remake/', name: '鬼武者：高清复刻版' },
  { url: '/2004/The-Legend-Of-Heroes-6-Sora-no-Kiseki-FC/', name: '英雄传说6：空之轨迹FC' },
  { url: '/2015/Battlefleet-Gothic-Armada/', name: '哥特舰队：阿玛达' },
  { url: '/2018/battlefleet-gothic-armada-2/', name: '哥特舰队：阿玛达2' },
  { url: '/2018/devil-may-cry-hd-collection/', name: '鬼泣HD合集' },
  { url: '/2015/Devil-May-Cry-4-Special-Edition/', name: '鬼泣4：特别版' },
  { url: '/2013/Devil-May-Cry/', name: 'DmC：鬼泣' },
  { url: '/2011/Rage/', name: '狂怒' },
  { url: '/2012/Sleeping-Dogs/', name: '热血无赖' },
  { url: '/2009/Torchlight/', name: '火炬之光' },
  { url: '/2012/Torchlight-II/', name: '火炬之光2' },
  { url: '/2018/sheng-nv-zhan-qi/', name: '圣女战旗' },
  { url: '/2017/musou-stars/', name: '无双全明星' },
  { url: '/2004/Star-Wars-Battlefront/', name: '星球大战：前线' },
  { url: '/2017/star-wars-battlefront-2/', name: '星球大战：前线2' },
  { url: '/2013/Shin-Sangoku-Musou-7-Moushouden/', name: '真三国无双7：猛将传' },
  { url: '/2019/shadows-die-twice/', name: '只狼：影逝二度' },
  { url: '/2009/Borderlands/', name: '无主之地' },
  { url: '/2012/Borderlands-2/', name: '无主之地2' },
  { url: '/2015/Borderlands-3/', name: '无主之地3' },
  { url: '/2015/Homeworld-Remastered-Collection/', name: '家园：高清重制版合集' },
  { url: '/2016/homeworld-deserts-of-kharak/', name: '家园：卡拉克沙漠' },
  { url: '/2018/cliff-empire/', name: '悬崖帝国' },
  { url: '/2017/thesurge/', name: '迸发' },
  { url: '/2019/the-surge-2/', name: '迸发2' },
  { url: '/2009/Spore-Galactic-Adventures/', name: '孢子银河冒险' },
  { url: '/2008/my-sims/', name: '我的模拟人生' },
  { url: '/2016/overcooked/', name: '胡闹厨房' },
  { url: '/2019/overcooked2/', name: '胡闹厨房2' },
  { url: '/2018/rise-of-industry/', name: '工业崛起' },
  { url: '/2019/transport-fever-2/', name: '狂热运输2' },
  { url: '/2017/frostpunk/', name: '冰汽时代' },
  { url: '/2009/Trine/', name: '三位一体' },
  { url: '/2011/Trine-2/', name: '三位一体2' },
  { url: '/2012/Trine-2-Goblin-Menace/', name: '三位一体2：哥布林的威胁' },
  { url: '/2015/Trine-3-The-Artifacts-of-Power/', name: '三位一体3：权力圣器' },
  { url: '/2019/trine-4-the-nightmare-prince/', name: '三位一体4：梦魇王子' },
  { url: '/2016/bright-memory/', name: '光明记忆：无限' },
  { url: '/2017/nine-parchments/', name: '九张羊皮纸' },
  { url: '/2017/startup-company/', name: '初创公司' },
  { url: '/2015/Assassins-Creed-Chronicles-China/', name: '刺客信条编年史：中国' },
  { url: '/2015/Assassins-Creed-Chronicles-India/', name: '刺客信条编年史：印度' },
  { url: '/2015/Assassins-Creed-Chronicles-Russia/', name: '刺客信条编年史：俄罗斯' },
]

const superagent = require('superagent')
const cheerio = require('cheerio')
const gameService = require('../service/game')

function get(){
  var name = '模拟人生4'
  var url = config.kuBaseUrl + '/2014/The-Sims-4/'
  var result = await gameService.getModel(name)
  if (result !== null) {
    ctx.send({ success: true, message: '获取数据成功', data: result })
  } else {
    ctx.send({ success: false, message: '获取数据失败', data: result })
  }
}

function papapa(){
  // var authCode = ctx.request.body.code
  // if (typeof authCode === 'undefined' || authCode === '' || authCode !== 'xwltz') {
  //   ctx.send({ success: false, message: '授权失败' })
  //   return
  // }
  var url = ctx.request.query.url
  // var url = '/2014/The-Sims-4/'

  var finalUrl = config.kuBaseUrl + url
  var result = getHtml(finalUrl)
  var model = html2Model(result, finalUrl)
  var isSuccess = await gameService.modelSaveOrUpdate(model)
  ctx.send({ success: isSuccess, message: 'request success' })
  return
}

function getHtml(url) {
  try {
    const result = await superagent.get(url).accept(config.accept).set('Referer', config.referer).set('User-Agent', userAgentClass.GetRondom())
    return result
  } catch (error) {
    console.log(error)
    return ''
  }
}

function html2Model(result, url) {
  var gameObject = new Object()
  //加载页面DOM
  var $ = cheerio.load(result.text, {
    decodeEntities: false,
    ignoreWhitespace: false,
    xmlMode: false,
    lowerCaseTags: false,
  })

  gameObject.chsName = $('.tit_CH').text() // 中文名
  gameObject.engName = $('.tit_EN').text() // 英文名
  gameObject.img = $('.YXXX-L img').attr('src') // 封面图

  gameObject.platform = [] // 平台
  $('.win a').each((index, value) => {
    var platformItem = {}
    var $aText = $(value).text()
    var $aTime = $(value).data('time')
    platformItem.text = $aText
    platformItem.time = $aTime
    gameObject.platform.push(platformItem)
  })

  gameObject.clock = $('.clock').text().trim().replace(' ', '') // 主线时间
  gameObject.type = $('.div3 .tt2 .tag>a').text() // 游戏类型
  gameObject.chinese = $('.div3 .tt1 .txt').text() // 是否支持中文
  gameObject.publisher = $('.div3 .tt2 .txt').text().split('/')[1] // 发行制作公司
  gameObject.developer = $('.div3 .tt2 .txt').text().split('/')[0] // 发行制作公司
  gameObject.content = $('.YXXX-R .div4').text().trim().replace(' ', '') // 简介

  gameObject.picture = [] // 图册
  $('.piclist a').each((index, value) => {
    var pictureItem = {}
    var $pic = $(value).data('pic').trim()
    var $picT = $pic.replace('small', 'tiny')
    var $picS = $pic.replace('small', 'small')
    var $picO = $pic.replace('_small', '').replace('small_', '')
    pictureItem.tiny = $picT
    pictureItem.small = $picS
    pictureItem.original = $picO
    gameObject.picture.push(pictureItem)
  })

  gameObject.video = [] // 视频
  $('.vodeolist a').each((index, value) => {
    var videoItem = {}
    var $vId = $(value).data('vid').trim()
    var $vImg = $(value).find('.img img').attr('src')
    var $vTitle = $(value).find('.con .tit').text()
    var $vTime = $(value).find('.con .time').text()
    var $vUrl = config.videoBaseUrl + $vId
    videoItem.img = $vImg
    videoItem.title = $vTitle
    videoItem.time = $vTime
    videoItem.url = $vUrl
    gameObject.video.push(videoItem)
  })

  gameObject.kuUrl = url
  return gameObject
}